package com.danielschon.triangularity.utility;

import java.util.ArrayList;

import org.andengine.entity.primitive.Line;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

import android.util.Log;

import com.danielschon.triangularity.activities.MainActivity;
import com.danielschon.triangularity.sprites.Ship;

public class Tail {
	int length = 64;
	int interval = 4;
	Ship ship;
	
	ArrayList<Vec> vecPath;
	ArrayList<Line> linePath;
	VertexBufferObjectManager lineVBOM;
	
	ArrayList<Color> colSequence;
	int sequencePos = 0;
	/**
	 * it's the tail's constructor dude
	 * @param minVertexInterval
	 * @param tailLength
	 * @param shipParent
	 */
	
	public Tail(int minVertexInterval, int tailLength, Ship shipParent){
		length = tailLength;
		interval = minVertexInterval;
		ship = shipParent;
		
		vecPath = new ArrayList<Vec>();
		linePath = new ArrayList<Line>();
		colSequence = new ArrayList<Color>();
		lineVBOM = ship.activity.getVertexBufferObjectManager();
	}
	
	/**
	 * Add an item to the color sequence
	 * @param color
	 */
	
	public void addColor(Color c){
		colSequence.add(c);
	}
	
	/**
	 * To be called every frame to test if the tail needs to be modified
	 * @param isWrap 
	 */
	
	public void check(boolean isWrap){
		/* If the color sequence is blank, make it solid black */
		if (colSequence.size() == 0){
			colSequence.add(new Color(1,1,1));
		}
		
		Vec lastWaypoint;
		if (vecPath.size() > 0){
			lastWaypoint = vecPath.get(vecPath.size()-1);
		}else{
			lastWaypoint = new Vec(MainActivity.CAMERA_WIDTH/2 - 32, MainActivity.CAMERA_HEIGHT/2 - 32);
		}
		float dist = lastWaypoint.getDistanceTo(ship.location);
		
		/* Update the path */
		if (dist >= interval){
			vecPath.add(ship.location.clone());
			addPathPoint(isWrap);
			
		}
		if (vecPath.size() > length){
			removePathCaboose();
		}
	}
	
	void addPathPoint(boolean isWrap) {
		Line line;
		
		/* Calculate line points */
		Vec previous;
		int size = vecPath.size();
		if (size > 1){
			previous = vecPath.get(size-2);
		}else{
			previous = new Vec(MainActivity.CAMERA_WIDTH/2, MainActivity.CAMERA_HEIGHT/2);
		}
		Vec current = vecPath.get(size-1);
		
		/* Add to arrays */
		line = new Line(previous.x, previous.y, current.x, current.y, lineVBOM);
		
		/* Get color from colSequence */
		line.setColor(colSequence.get(sequencePos));
		sequencePos++;
		if (sequencePos >= colSequence.size()){
			sequencePos = 0;
		}
		line.setLineWidth(4);
		
		if (isWrap) {
			line.setAlpha(0);
			ship.resetIsWrap();
		}
		
		linePath.add(line);
		
		ship.activity.scene.attachChild(line);
	}
	
	void removePathCaboose(){
		Line caboose = linePath.get(0);
		ship.activity.destroyEntity(caboose);
		vecPath.remove(0);
		linePath.remove(0);
	}
	
	public void reset(){
		while(vecPath.size() > 1){
			removePathCaboose();
			Log.w("tail length:",""+vecPath.size());
		}
	}
}
